package com.springboot.ding.monitor;

import com.springboot.ding.monitor.message.TextMessage;
import com.springboot.ding.DingConfigure;
import com.springboot.ding.DingSecret;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * @description:
 * @author: zyj
 * @create: 2020-07-27 17:42
 **/
public class MonitorUtil {

    static Logger logger = LoggerFactory.getLogger(MonitorUtil.class);

    /**
     * @Description: 发送普通文本警告
     * @Param: [phones, content, isAtAll]
     * @return: void
     * @Author: zyj
     * @Date: 2020/7/27
     */
    public static void sendMessageTextStr( Exception e, String message, Logger logger, boolean sendDing, boolean sendCat){
        /**发送钉钉*/
        if(sendDing){
            List<String> content = new ArrayList<>();
            content.add(message);
            content.add(e.toString());
            MonitorUtil.sendMessageText(content);
        }

        /**发送cat*/
        if(sendCat) {
            sendCat(e, message);
        }

        logger.error(message, e);
    }


    /**
     * @Description: sendCat
     * @Param: [phones, content, isAtAll]
     * @return: void
     * @Author: zyj
     * @Date: 2020/7/27
     */
    public static void sendCat(Exception exception,  String message){
        try {
            ExecutorService executorService = MonitorThreaPool.getInstance();
            executorService.execute(new PrintException(exception, message));


        } catch (Exception e){
            logger.error("打印普通文本警告失败：", e);
        }
    }

    /**
     * @Description: 发送普通文本警告
     * @Param: [phones, content, isAtAll]
     * @return: void
     * @Author: zyj
     * @Date: 2020/7/27
     */
    public static void sendMessageTextStr(String message){
        List<String> content = new ArrayList<>();
        content.add(message);
        MonitorUtil.sendMessageText(content);
        sendMessageText(null, content, true);
    }



    /**
     * @Description: 发送普通文本警告
     * @Param: [phones, content, isAtAll]
     * @return: void
     * @Author: zyj
     * @Date: 2020/7/27
     */
    public static void sendMessageText(List<String> content){
        sendMessageText(null, content, true);
    }

    /**
    * @Description: 发送普通文本警告list
    * @Param: [phones, content, isAtAll]
    * @return: void
    * @Author: zyj
    * @Date: 2020/7/27
    */
    public static void sendMessageText(List<String> phones, List<String> content, Boolean isAtAll){
        try {
            DingConfigure dingConfigure = getConfig();
            if(null == dingConfigure){
                return;
            }

            ExecutorService executorService = MonitorThreaPool.getInstance();
            String[] secrets = DingSecret.getSecret(dingConfigure.getSecret());
            TextMessage textMessage = new TextMessage(phones, content, isAtAll);
            textMessage.setAccessToken(dingConfigure.getToken());
            textMessage.setTimestamp(secrets[0]);
            textMessage.setSign(secrets[1]);
            executorService.execute(new MonitorTask(textMessage));

        } catch (Exception e){
            logger.error("发送普通文本警告list失败：", e);
        }
    }

    /**
    * @Description: 获取配置文件
    * @Param: []
    * @return: com.springboot.util.ding.DingConfigure
    * @Author: zyj
    * @Date: 2020/7/27
    */
    public static DingConfigure getConfig(){
        return DingConfigure.getBean("dingConfigure", DingConfigure.class);
    }
}
